/* 
 * Control Panel js functions
 */


function MappsieClient(server_url, type){
    this.type = "POST" || "GET";
    this.URL = server_url || "http://localhost/mappsie/index.php/api/";
    this.serverRequest = function(url, args ){
        var result;
        $.ajax({
            type: this.type,
            async: false,
            dataType:"json",
            url: this.URL + url,
            data: args,
            success: function(data){
                result = data;
            }
        });
        return result;
    }
    
    /* User functions */
    this.login = function(_mobile,_password){
        return this.serverRequest("login/", {mobile: _mobile, password: _password});
    };
    
    this.getUser = function(_id){
        return this.serverRequest("getUser/", {id: _id});
    };
    this.getUserByMobile = function(number){
        return this.serverRequest("getUserByMobile/", {mobile: number});
    };
    
    this.insertDeal = function(obj){
        return this.serverRequest("createMultipleDeals/", obj);
    }
    
    /**
     *  this method takes serialized data and 
     *  stores it in the db. 
     *  
     *  The object passed must contain the model parameter specifying the table and the remaining parameters matching database fields names.
     *  
     *  Available models are: admin,booked_deal, category, clipped_deal, deal, feedback, place, user
     *  
     *  The PHP web service then associates relevant properties to an object and stores it in the db.
     *  
     *  Usage example
     *  
     *  client.insert(
     *  { 
     *      model: "user", 
     *      fullname : "Iolo Pearse",
     *      mobile: "08712334567", 
     *      email: "iolo@iolo.ie",
     *      password: "letmein"
     *      }
     *  );
     *  
     *  For the name of the parameters of each table, refer to the sql file.
     *  
     */
    
    this.insert = function(obj){
        return this.serverRequest("insert/",obj);
    }
    
    this.update = function(obj){
        return this.serverRequest("update/", obj);
    }
    
    // can't use the name 'delete' for method as it's a reserved word
    this.deleteObject = function(obj){
        return this.serverRequest("delete/", obj);
    }
    
    /**
     * specify model and id or just model for a list of all the db objects
     * i.e. client.get({model: 'user'}); // gets all users
     * client.get({ model: 'user', id: 1}); // gets user with id 1
     */
    this.get = function(obj){
        return this.serverRequest("get/", obj);
    }
    
    /**
     * obj needs to pass town_id
     * i.e. :
     * client.getDealsForToday({town_id:2});
     */
    this.getDealsForToday = function(obj){
        return this.serverRequest("getDealsForToday/",obj);
    }
    
    this.getTownsForCounty = function(obj){
        return this.serverRequest("getTownsForCounty/", obj);
    }

}




